package org.upfrost.util;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;

public class Tuple<T> implements Serializable 
{
	private T[] m_data;
	
	public Tuple(T... data)
	{
		m_data = data;
	}
	
	public T[] data()
	{
		return m_data;
	}
	
	public int size()
	{
		return m_data.length;
	}
	
	public T get(int index)
	{
		return m_data[index];
	}

	@Override
	public boolean equals(Object obj) 
	{
		if (obj instanceof Tuple)
		{
			return Arrays.deepEquals(m_data, ((Tuple)obj).m_data);
		}
		
		return false;
	}

	@Override
	public int hashCode() 
	{
		return Arrays.hashCode(m_data);
	}

	static public Tuple createMerge(Tuple<?> first, Tuple<?>... other)
	{
		int totalsize = first.m_data.length;
		for (Tuple t: other)
			totalsize += t.m_data.length;
		
		Object[] result = (Object[]) Array.newInstance(Object.class, totalsize);

		int index = 0;
		for (; index < first.m_data.length; index++)
			result[index] = first.m_data[index];
		
		for (Tuple<?> t: other)
		{
			for (int i=0; i<t.m_data.length; i++)
			{
				result[index+i] = t.m_data[i];
			}
			index += t.m_data.length;
		}
		
		return new Tuple(result);
	}
	
}
